package com.hg.controller;

import com.hg.pojo.Item;
import com.hg.service.IItemService;
import com.hg.util.PageResult;
import com.hg.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Baomidou
 * @since 2025-07-30
 */
@RestController
@RequestMapping("/backend/item")
public class ItemController {
    @Autowired
    private IItemService itemService;

    @RequestMapping("/selectItemInfo")
    public Item selectItemInfo(Long itemId) {
        return itemService.getById(itemId);
    }

    @RequestMapping("/selectTbItemAllByPage")
    public Result selectTbItemAllByPage(@RequestParam(defaultValue = "1") Integer page,
                                        @RequestParam(defaultValue = "5") Integer rows) {
        try {
            PageResult pageResult = itemService.selectTbItemAllByPage(page,rows);
            return Result.ok(pageResult);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("查询失败");
        }
    }

    @RequestMapping("/insertTbItem")
    public Result insertTbItem(Item item, String desc, String itemParams){
        try {
            itemService.insertTbItem(item,desc,itemParams);
            return  Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("添加失败");
        }
    }

    @RequestMapping("/deleteItemById")
    public Result deleteItemById(Long itemId){
        try {
            itemService.deleteItemById(itemId);
            return  Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
    }

    @RequestMapping("/preUpdateItem")
    public Result preUpdateItem(Long itemId){
        try {
            Map map= itemService.preUpdateItem(itemId);
            return Result.ok(map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("预更新失败");
        }
    }

    @RequestMapping("/updateTbItem")
    public Result updateTbItem(Item item, String desc, String itemParams){
        try {
            itemService.updateTbItem(item,desc,itemParams);
            return  Result.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("更新失败");
        }
    }



}
